$(function () {
    //获取token
    var token = localStorage.token;
    //加载地址列表
    $.ajax({
        type: "get",
        url: domain.test + "/getaddresslist",
        beforeSend: function (xhr) {
            xhr.setRequestHeader("token", token);
        },
        success: function (response) {
            console.log(response);
            if (response.data.rtnCode === 200) {
                var addressList = response.data.data;
                console.log(addressList);
                $(".addressList").html(template("addressList", { addressList: addressList }));
            }
            if (response.data.rtnCode === 401 || response.data.rtnCode === 400) {
                var outTimeCode = response.data.rtnCode;
                location.href = "../../index.html?outTimeCode=" + outTimeCode;
            }
        }
    });
    //设置默认地址
    $(".addressList").on("click", ".adminAdd", function (e) {
        e.stopPropagation();
        var status = parseInt($(this).find(".hidden").val());
        if (status === 1) {
            return false;
        }
        var addressId = $(this).parent().parent().attr("data-addressId");
        console.log(addressId);
        var regionId = $(this).parent().parent().attr("data-regionId");
        var contact = $(this).parent().parent().find(".person").attr("data-contcat");
        console.log(contact);
        var mobile = $(this).parent().parent().find(".tel").attr("data-mobile");
        console.log(mobile);
        var address = $(this).parent().parent().find(".address").attr("data-address");
        console.log(address);
        var region = $(this).parent().parent().find(".address").attr("data-region");
        console.log(region);
        $.ajax({
            type: "post",
            url: domain.test + "/address",
            data: {
                id: addressId,
                contact: contact,
                mobile: mobile,
                address: address,
                regionId: regionId,
                default: 1
            },
            beforeSend: function (xhr) {
                xhr.setRequestHeader("token", token);
            },
            success: function (response) {
                console.log(response);
                if (response.data.rtnCode === 200) {
                    location.reload();
                }
                if (response.data.rtnCode === 401 || response.data.rtnCode === 400) {
                    var outTimeCode = response.data.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            }
        });
    });
    //删除地址
    var regionId;
    $(".addressList").on("click",".del",function() {
        regionId =  $(this).parent().parent().parent().attr("data-addressId");
        var width = window.screen.width;
        var height = window.screen.height;
        $(".logoutMarsk").css({
            "width":width,
            "height":height,
            "display":"block"
        });
    });
    //取消删除
    $(".cancel").on("click",function() {
        $(".logoutMarsk").css("display","none");
    });
    $(".sure").on("click",function() {
        console.log(regionId);
        $.ajax({
            type: "delete",
            url: domain.test + `/deladdress?id=${regionId}`,
            beforeSend: function(xhr) {
                xhr.setRequestHeader("token",token);
            },
            success: function (response) {
                console.log(response);
                if(response.data.rtnCode===200) {
                    location.reload();
                }
            }
        });
    });
    //编辑地址
    $('.addressList').on("click",".edit",function(e) {
        e.stopPropagation();
        var status = parseInt($(this).parent().parent().find(".hidden").val());
        console.log(status);
        var addressId = $(this).parent().parent().parent().attr("data-addressId");
        console.log(addressId);
        var regionId = $(this).parent().parent().parent().attr("data-regionId");
        var contact = $(this).parent().parent().parent().find(".person").attr("data-contcat");
        console.log(contact);
        var mobile = $(this).parent().parent().parent().find(".tel").attr("data-mobile");
        console.log(mobile);
        var address = $(this).parent().parent().parent().find(".address").attr("data-address");
        console.log(address);
        var region = $(this).parent().parent().parent().find(".address").attr("data-region");
        console.log(region);
        localStorage.editStatus = status;
        localStorage.editAddressId = addressId;
        localStorage.editRegionId = regionId;
        localStorage.editContact = contact;
        localStorage.editMobile = mobile;
        localStorage.editAddress = address;
        localStorage.editRegion = region;
        location.href = "./addAddress.html";
    });
    //添加地址跳转
    $(".addAdressBtn").on("click",function(){
        localStorage.editStatus = "";
        localStorage.editAddressId = "";
        localStorage.editRegionId = "";
        localStorage.editContact = "";
        localStorage.editMobile = "";
        localStorage.editAddress = "";
        localStorage.editRegion = "";
        location.href = "./addAddress.html"
    });
});